<script language="javascript" type="text/javascript">
	function validateForm() {

	var title = document.getElementById("title");
		title.style.backgroundColor="#fff";		
	
		if(title.value == ''){
		alert('Vui lòng nhập tiêu đề!');
		title.focus();
		title.style.backgroundColor="#F90";
		return false;
		}			
		
	}
</script>
<?php
	$id=$_GET['id'];
	$tylesave = 'none';
	if(isset($_POST['submit']))
	{		
		$title=$_POST['title'];		
		$ordering=$_POST['ordering'];
		$published=$_POST['published'];
		$showtitle=$_POST['showtitle'];
		$content=$_POST['content'];
		
		$params=$_POST['params'];

		$prupdate = '';
		$i=1;
		$tongprs = count($params);
		foreach($params AS $key=>$pr2)
		{
			$pr3 = mysql_real_escape_string(str_replace("\r\n","\\n",$pr2));
			$prupdate .= $key."=".$pr3;		
			if($i < $tongprs){
			$prupdate .= '\n';	
			}
			
			$i++;
		}
		
		if(isset($id) && $id >0)
		{
			$sql="update dm_modules set 
									   title='$title',
									   content='$content',
									   ordering='$ordering',
									   `published`='$published',
									   showtitle='$showtitle',
									   `params`='$prupdate'
										where id=".$id;
			
			// echo	$sql;
			mysql_query($sql);
			$tylesave = 'block';
		}		
	}
?>

<!-- Container -->
<div id="container">
	<div class="shell">
		<div id="saveok" style="display:<?php echo $tylesave?>" class="msg msg-ok">
			<p><strong>Chức năng được lưu thành công!</strong></p>
			<a href="#" class="close">close</a>
		</div>
		<!-- Main -->
		<div id="main">
			<div class="cl">&nbsp;</div>
			
			<!-- Content -->
			<div id="content">
				<!-- Box -->
				<div class="box">
					<!-- Box Head -->
					<div class="box-head">
						<h2>Thêm bài viết</h2>
					</div>
					<!-- End Box Head -->
					
					<form action="#" id="frmparams" method="post" enctype="multipart/form-data" onSubmit="return validateForm();">
						
						<!-- Form -->
						<div class="form_module">
							<?php
				$id = $_GET['id'];
				$sql="select * from dm_modules where id=$id";
				//echo $sql;
				$rs=mysql_query($sql);
				$row=mysql_fetch_assoc($rs);
				$modulename = $row['module'];
				
				echo "<div class='admin_params'>";
				if($modulename =='custom')
				{
					echo "<textarea name='content' id='content_custom' >$row[content]</textarea>";
				}else{
					echo "<input  type='hidden' value='' name='content' />";
				}
				
				
				
				$linkXml=PATH_ROOT_ADMIN.DS.'params'.DS.$modulename.'.xml';
				$xmlData = simplexml_load_file($linkXml);
		
				$params = $row['params'];
				
					$paramsarr = explode("\n",$params);
								

					
				
				
				foreach($xmlData->param AS $xml)
				{				
					foreach($paramsarr AS $pr )
					{
						if(trim($pr)!='')
						{
							$prrr = explode("=",$pr);
							$prname = $prrr[0];
							$prvalue = $prrr[1];
							if(trim($prname) == trim($xml['name']))
							{
								if($xml['type'] =='hidden')
								{
									echo "<input type='$xml[type]' value='$prvalue' name=\"params[$xml[name]]\" />";
								}else
								{
								
									echo "<div class='admin_label'>$xml[label]</div>";
									echo "<div class='admin_input'>";
									if($xml['type'] =='textarea')
									{
										$vlbr = str_replace('\n',"\n",$prvalue);
										// $vlbr = str_replace('^*^',"\n",$prvalue);
										echo "<textarea name=\"params[$xml[name]]\" rows='$xml[rows]' cols='$xml[cols]' $xml[style]>$vlbr</textarea>";
									}else
									if($xml['type'] =='radio')
									{
										foreach($xml->option AS $op)
										{
											if(trim($prvalue)==$op['value'])
											{									
												$check =' checked ';
											}else{
												$check ='';
											}
											
											echo "<span style='margin-right:30px'><label for='$op[value]'>$op</label>";
											echo "<input type='radio' value='$op[value]' $check id='$op[value]' name=\"params[$xml[name]]\" $xml[style] />";
											echo "</span>";
												
										}
									}else
									if($xml['type'] =='list')
									{
										echo "<select name=\"params[$xml[name]]\" >";
										foreach($xml->option AS $op)
										{
											if(trim($prvalue)==$op['value'])
											{									
												$select =' selected ';
											}else{
												$select ='';
											}
											
											echo "<option value='$op[value]' $select >$op</option>";						
											
										}
										echo "</select>";
									}
									else{									
										echo "<input class='input_module' type='$xml[type]' value='$prvalue' name=\"params[$xml[name]]\" $xml[style] />";
									}
									echo "</div>";
								}
							}	
							
						}
					}					
				}				
				?>		
	</div>

						</div>
						</div>
										
				</div>
				<!-- End Box -->

			</div>
			<!-- End Content -->
			
			<!-- Sidebar -->
			<div id="sidebar">
				
				<!-- Box -->
				<div class="box">
					
					<!-- Box Head -->
					<div class="box-head">
						<h2>Chức năng</h2>
					</div>
					<!-- End Box Head-->

					<div class="box-content">
						<div class="buttons">
							<input type="button" onclick="window.location.href='?action=params'" class="button" value="Thoát" />
							<!--<input type="button" onclick="validateForm()" name="submit" class="button" value="Lưu" />-->
							<input type="submit"  name="submit" class="button" value="Cập nhật" />
						</div>
	
						
						<!-- Sort -->
						<div class="sort">								

							<label>Tiêu đề</label>						
							<input type="text" name="title" id="title"  value="<?php echo $row['title'] ?>" class="field" />
							<br/>
							<label>Thứ tự</label>		
							<input type="text" name="ordering" value="<?php echo $row['ordering'] ?>" class="field" />
							<br/>							
							<label>Bật/tắt</label>	
							<div style="margin:16px;  background-color: gainsboro; height: 17px;">
							<?php
							if(!isset($row['published']))
							{
								$checked0 = "";
								$checked1 = "CHECKED";
							}else{
								if($row['published']==1)
								{
									$checked0 = "";
									$checked1 = "CHECKED";
								}else{
									$checked0 = "CHECKED";
									$checked1 = "";
								}
							}
							?>
							<span style="margin-right:10px; margin-left: 20px;">Bật</span><INPUT TYPE=radio <?php echo $checked1?>  NAME="published" VALUE="1">&nbsp &nbsp &nbsp &nbsp &nbsp
							<span style="margin-right:10px">Tắt</span><INPUT TYPE=radio <?php echo $checked0?> NAME="published" VALUE="0">
							</div>	
							
							<br/>
							<label>Ẩn/hiện tiêu đề</label>	
							<div style="margin:16px;  background-color: gainsboro; height: 17px;">
							<?php
							if(!isset($row['showtitle']))
							{
								$checked0 = "";
								$checked1 = "CHECKED";
							}else{
								if($row['showtitle']==1)
								{
									$checked0 = "";
									$checked1 = "CHECKED";
								}else{
									$checked0 = "CHECKED";
									$checked1 = "";
								}
							}
							?>
							<span style="margin-right:10px; margin-left: 20px;">Bật</span><INPUT TYPE=radio <?php echo $checked1?>  NAME="showtitle" VALUE="1">&nbsp &nbsp &nbsp &nbsp &nbsp
							<span style="margin-right:10px">Tắt</span><INPUT TYPE=radio <?php echo $checked0?> NAME="showtitle" VALUE="0">
							
							
							</div>	

							<input type="hidden" value="1" name="created_by" />
										
						</div>
						<!-- End Sort -->
						
					</div>
				</div>
				<!-- End Box -->
			</div>
			<!-- End Sidebar -->
			</form>
			<div class="cl">&nbsp;</div>			
		</div>
		<!-- Main -->
	</div>
</div>

<script>
	var editor=CKEDITOR.replace( 'content_custom' );
	CKFinder.setupCKEditor( editor, '../libs/ckfinder/' ) ;

</script>